package com.wang.ssm.controller;

import com.alibaba.fastjson.JSON;
import com.wang.ssm.pojo.Question;
import com.wang.ssm.pojo.Score;
import com.wang.ssm.pojo.Stuan;
import com.wang.ssm.service.QuestionService;
import com.wang.ssm.service.StuService;
import com.wang.ssm.util.Result;
import com.wang.ssm.util.StatusCode;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author 王瑞杰
 * date 2023/10/28
 */
@RestController
@RequestMapping("/question")
@CrossOrigin
public class QuestionController {
    @Resource
    private QuestionService service;
    @Transactional
    @GetMapping("/scores")
    public Result findscore(String uid){
        return  new Result<>(200,"查询OK",true,this.service.findscore(uid));
    }

    @PostMapping("/addscore")
    public Result addscore(@RequestBody Score score){
        return service.addscore(score)
                ? new Result<>(StatusCode.OK,"操作成功",true)
                :new Result<>(StatusCode.ERROR,"操作失败",false);
    }
    @PostMapping("/addstuan")
    public Result addstuan(@RequestBody Stuan[] stuan){
        List<Stuan> list = new ArrayList<Stuan>();
        Collections.addAll(list,stuan);
        boolean flag=service.addstuan(list);
        return flag ? new Result<>(StatusCode.OK,"操作成功",true)
                :new Result<>(StatusCode.ERROR,"操作失败",true);
    }

    @GetMapping("/questions")
    public Result findAll(){
        return  new Result<>(200,"查询OK",true,this.service.findAll());
    }

    @PostMapping("/save")
    public Result save(@RequestBody Question[] formLabelAlign){
        List<Question> list = new ArrayList<Question>();
        Collections.addAll(list,formLabelAlign);
        System.out.println(list);
        boolean flag=service.save(list);
        return flag ? new Result<>(StatusCode.OK,"操作成功",true)
                :new Result<>(StatusCode.ERROR,"操作失败",true);
    }
    @DeleteMapping("/del")
    public Result del(String[] arr){
        return service.del(arr)
                ? new Result<>(StatusCode.OK,"操作成功",true)
                :new Result<>(StatusCode.ERROR,"操作失败",false);
    }

    @PostMapping ("/stuans")
    public Result stuans(@RequestBody Stuan stuan){
        return  new Result<>(200,"查询OK",true,this.service.stuans(stuan));
    }

}
